package com.youease.chris.control
{
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.model.proxy.MapDataProxy;
	import com.youease.chris.model.vo.LayerData;
	import com.youease.chris.model.vo.TextureData;
	import com.youease.chris.model.vo.TileData;
	import com.youease.chris.view.mediator.MapEditMediator;
	import com.youease.chris.view.mediator.MapPropertiesMediator;
	import com.youease.chris.view.mediator.MapSceneMediator;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	public class RandomMapCommand extends SimpleCommand implements ICommand
	{
		public static const RANDOM_MAP:String = "random_map";
		
		public function RandomMapCommand()
		{
			super();
		}
		
		override public function execute(notification:INotification):void {

			var mapDataProxy:MapDataProxy = this.facade.retrieveProxy(GlobalObject.currentMap) as MapDataProxy;
			for each(var layer:LayerData in mapDataProxy.mapData.layers) {
				
				var len:int = layer.tiles.length;
				var arr:Array = [];
				var i:int = 0;
				var textTure:TextureData;
				
				for each(var random:XML in GlobalObject.config.child("randoms").child("random")) {
					var type:int = int(random.@type);
					var pics:int = int(random.@pics);
					var rate:Number = Number(random.@rate);
					
					for(i=0;i<len*rate;i++) {
						textTure = new TextureData();
						textTure.type = type;
						textTure.index = int(Math.random()*pics+1)
						textTure.img = GlobalObject.texturesEnName[type.toString()]+(textTure.index>9?textTure.index:("0"+textTure.index))+".png";
						arr.push(textTure);
					}
				}
				
				for(i=0;i<len;i++) {
					arr[i] = {index:Math.random()*len,texture:arr[i]};
				}
				
				arr.sortOn("index");
				for(i=0;i<len;i++) {
					var tile:TileData = layer.tiles[i];
					tile.textrue = arr[i].texture;
				}
			}
			
			Alert.show("随机地图已经生成，请保存后重新打开工程");
		}
	}
}